package KK.System
{
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	
	import mx.core.Singleton;
	
	public class ApplicationManager implements IApplicationManager
	{
		static private var _instance:ApplicationManager;//获取单例
		private var _appList:Dictionary;
		private var _resList:Dictionary; 
		
		private var _application:Vector.<ApplicationDomain>;
		public function ApplicationManager(singleton:Singleton)
		{
			if(_instance){
				throw new Error("this singleton class pless using Instance Get Instance.");
			}
			_instance=this;
			init();
		}
		
		private function init():void{
			_application=new Vector.<ApplicationDomain>;
			_appList=new Dictionary();
			
			_resList=new Dictionary();
			
		}
		/**
		 *  添加ApplicationDomain
		 * @param domain app 名
		 * @param app 
		 * @param int 0添加成功,非0失败
		 */	
		public function AddApplication(domain:String,app:ApplicationDomain):int{
			var result:int=1;
			if(_appList)
			{
				_appList[domain]=app;
				result=0;
			}
			return result;
		}
		
		
		
		
		public function AddLoadedList(domain:String,event:Event):int{
			var result:int=1;
			if(_resList)
			{
				_resList[domain]=event;
			}
			
			return result;
		}
		
		/**
		 * 添加applicationdomain 
		 * @param app
		 * 
		 */
		public function add(app:ApplicationDomain):void{
			_application.push(app);
		}
		
		
		/**
		 * 获取类 
		 * @param name
		 * @return 
		 * 
		 */
		public function getClass(name:String):Class{
			var result:Class;
			var i:int=0;
			var len:int=_application.length;
			while(i<len){
				if(_application[i].hasDefinition(name)){
					result=_application[i].getDefinition(name) as Class;
					break;
				}
				i++;
			}
			
			return result;
		}
		
		/**
		 * Get Application Domain 
		 * @param domain 记录名
		 * @return 返回对应application domain
		 * 
		 */
		
		public function GetApplication(domain:String):ApplicationDomain{
			var result:ApplicationDomain=null;
			if(_appList)
			{
				result=_appList[domain];
			}

			return result;
		}
		
		public function GetLoaded(domain:String):Event{
			var result:Event=null;
			if(_resList)
			{
				result=_resList[domain];
			}
			
			return result;
		}
		
		/**
		 * Remove Application Domain  
		 * @param domain 记录名
		 * @return  0成功，非0失败
		 * 
		 */
		public function RemoveLoaded(domain:String):int{
			var result:int=-1;
			if(_resList)
			{
				delete _resList[domain];
				result=0;
			}else{
				result=1;
			}
			return result;
		}
		
		
		/**
		 * Remove Application Domain  
		 * @param domain 记录名
		 * @return  0成功，非0失败
		 * 
		 */
		public function RemoveApplication(domain:String):int{
			var result:int=-1;
			if(_appList)
			{
				delete _appList[domain];
				result=0;
			}else{
				result=1;
			}
			return result;
		}
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * 添加 
		 * @param app
		 * 
		 */
		static public function Add(app:ApplicationDomain):void{
			Instance.add(app);
		}
		
		
		static public function GetClass(name:String):Class{
			return Instance.getClass(name);
		}
		

		/**
		 * 获取单例  
		 * @return 
		 * 
		 */
		static public function get Instance():ApplicationManager{
			if(!_instance){
				new ApplicationManager(new Singleton);
				
			}
			return _instance;
		}
		
	}
}

class Singleton{
	
}